package com.zhuobowen.warehouse.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.zhuobowen.common.idempotent.annotation.RepeatSubmit;
import com.zhuobowen.common.log.annotation.Log;
import com.zhuobowen.common.web.core.BaseController;
import com.zhuobowen.common.mybatis.core.page.PageQuery;
import com.zhuobowen.common.core.domain.R;
import com.zhuobowen.common.core.validate.AddGroup;
import com.zhuobowen.common.core.validate.EditGroup;
import com.zhuobowen.common.log.enums.BusinessType;
import com.zhuobowen.common.excel.utils.ExcelUtil;
import com.zhuobowen.warehouse.domain.vo.ImportBillDetailVo;
import com.zhuobowen.warehouse.domain.bo.ImportBillDetailBo;
import com.zhuobowen.warehouse.service.IImportBillDetailService;
import com.zhuobowen.common.mybatis.core.page.TableDataInfo;

/**
 * 入库单明细
 *
 * @author zbw
 * @date 2025-01-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/warehouse/importBillDetail")
public class ImportBillDetailController extends BaseController {

    private final IImportBillDetailService importBillDetailService;

    /**
     * 查询入库单明细列表
     */
    @SaCheckPermission("warehouse:importBillDetail:list")
    @GetMapping("/list")
    public TableDataInfo<ImportBillDetailVo> list(ImportBillDetailBo bo, PageQuery pageQuery) {
        return importBillDetailService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出入库单明细列表
     */
    @SaCheckPermission("warehouse:importBillDetail:export")
    @Log(title = "入库单明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ImportBillDetailBo bo, HttpServletResponse response) {
        List<ImportBillDetailVo> list = importBillDetailService.queryList(bo);
        ExcelUtil.exportExcel(list, "入库单明细", ImportBillDetailVo.class, response);
    }

    /**
     * 获取入库单明细详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("warehouse:importBillDetail:query")
    @GetMapping("/{id}")
    public R<ImportBillDetailVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(importBillDetailService.queryById(id));
    }

    /**
     * 新增入库单明细
     */
    @SaCheckPermission("warehouse:importBillDetail:add")
    @Log(title = "入库单明细", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ImportBillDetailBo bo) {
        return toAjax(importBillDetailService.insertByBo(bo));
    }

    /**
     * 修改入库单明细
     */
    @SaCheckPermission("warehouse:importBillDetail:edit")
    @Log(title = "入库单明细", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ImportBillDetailBo bo) {
        return toAjax(importBillDetailService.updateByBo(bo));
    }

    /**
     * 删除入库单明细
     *
     * @param ids 主键串
     */
    @SaCheckPermission("warehouse:importBillDetail:remove")
    @Log(title = "入库单明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(importBillDetailService.deleteWithValidByIds(List.of(ids), true));
    }
}
